﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Xsl;

namespace FoNetParser
{
    public static class Parser
    {
        public static void FoToRtf(XmlReader fo, string outputpath, string filename)
        {
            var output = string.Format(@"{0}\{1}.rtf", outputpath.TrimEnd('\\'), filename.Replace(".rtf", ""));

            #region XML Transform
            var xsltransform = new XslCompiledTransform();
            xsltransform.Load(Templates.XslFoToRtf);

            var docbuilder = new StringBuilder();
            var xmlwriter = new XmlTextWriter(new StringWriter(docbuilder));
            xsltransform.Transform(fo, xmlwriter);

            var writer = new StreamWriter(output);

            using (writer)
            {
                writer.Write(docbuilder.ToString());
            }

            #endregion
        }

        public static void HtmlToXslFo(XmlReader html, string outputpath, string filename)
        {
            var output = string.Format(@"{0}\{1}.fo", outputpath.TrimEnd('\\'), filename.Replace(".fo", ""));

            #region XML Transform
            var xsltransform = new XslCompiledTransform();
            xsltransform.Load(Templates.XhtmlToXslfo);

            var docbuilder = new StringBuilder();
            var xmlwriter = new XmlTextWriter(new StringWriter(docbuilder));
            xsltransform.Transform(html, xmlwriter);

            var writer = new StreamWriter(output);

            using (writer)
            {
                writer.Write(docbuilder.ToString());
            }

            #endregion
        }

        public static void FoToHtml(XmlReader fo, string outputpath, string filename)
        {
            var output = string.Format(@"{0}\{1}.html", outputpath.TrimEnd('\\'), filename.Replace(".html", ""));

            #region XML Transform
            var xsltransform = new XslCompiledTransform();
            xsltransform.Load(Templates.XslFoToXhtml);

            var docbuilder = new StringBuilder();
            var xmlwriter = new XmlTextWriter(new StringWriter(docbuilder));
            xsltransform.Transform(fo, xmlwriter);

            var writer = new StreamWriter(output);

            using (writer)
            {
                writer.Write(docbuilder.ToString());
            }

            #endregion
        }
    }
}
